package com.sc.nft.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.sc.nft.dao.ShareProfitDaoPoolDao;
import com.sc.nft.entity.ShareProfitDaoPool;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.ShareProfitDaoPoolService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 分账结算池 服务实现类
 * </p>
 *
 * @author liGen
 * @since 2024-11-26
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ShareProfitDaoPoolServiceImpl implements ShareProfitDaoPoolService {

    private final ShareProfitDaoPoolDao shareProfitDaoPoolDao;

    /**
     * 添加池中金额
     *
     * @param ecologyHatchId
     * @param collectionId
     * @param daoId
     * @param sourceDaoId
     * @param pointsId
     * @param amount
     * @param now
     * @return
     */
    @Override
    public Long addAmount(Long ecologyHatchId, Long collectionId, Long daoId, Long sourceDaoId, Long pointsId,
                          BigDecimal amount, Date now) {
        ShareProfitDaoPool shareProfitDaoPool = shareProfitDaoPoolDao.getOneByConditions(ecologyHatchId, collectionId, daoId, sourceDaoId, pointsId, now);
        if (shareProfitDaoPool == null) {
            DateTime beginOfMonth = DateUtil.beginOfMonth(now);
            DateTime endOfMonth = DateUtil.endOfMonth(now).offsetNew(DateField.SECOND, -1);
            shareProfitDaoPool = new ShareProfitDaoPool();
            shareProfitDaoPool.setEcologyHatchId(ecologyHatchId);
            shareProfitDaoPool.setCollectionId(collectionId);
            shareProfitDaoPool.setDaoId(daoId);
            shareProfitDaoPool.setSourceDaoId(sourceDaoId);
            shareProfitDaoPool.setPointsId(pointsId);
            shareProfitDaoPool.setAmount(amount);
            shareProfitDaoPool.setIsSettle(Boolean.FALSE);
            shareProfitDaoPool.setStartTime(beginOfMonth);
            shareProfitDaoPool.setEndTime(endOfMonth);
            shareProfitDaoPoolDao.save(shareProfitDaoPool);
        } else {
            if (shareProfitDaoPool.getIsSettle()) {
                // 已结算
                throw new GlobalRunTimeException("分账结算池已结算");
            }
            shareProfitDaoPoolDao.addAmount(shareProfitDaoPool.getId(), amount);
        }
        return shareProfitDaoPool.getId();
    }

    /**
     * 获取分账结算池中服务中心进入的池子
     *
     * @param ecologyHatchId
     * @param sourceDaoId
     * @return
     */
    @Override
    public List<ShareProfitDaoPool> getListByEcologyHatchIdAndSourceDaoId(Long ecologyHatchId, Long sourceDaoId, Boolean isSettle, Date now) {
        return shareProfitDaoPoolDao.getListByEcologyHatchIdAndSourceDaoId(ecologyHatchId, sourceDaoId, isSettle, now);
    }

    /**
     * 修改结算状态
     *
     * @param id
     * @param isSettle
     * @return
     */
    @Override
    public Boolean updateSettle(Long id, Boolean isSettle) {
        return shareProfitDaoPoolDao.updateSettle(id, isSettle);
    }
}
